﻿#include "memberdialog.h"
#include "ui_memberdialog.h"
#include<QTableWidgetItem>
#include <QDialogButtonBox>
#include<cmath>
#include<iostream>
#include<QMessageBox>
using namespace std;

#define DEBUG

MemberDialog::MemberDialog(MemberManage *  mm, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MemberDialog)
{
    ui->setupUi(this);
    this->mm = mm;

}

MemberDialog::~MemberDialog()
{
    delete ui;
}



void MemberDialog::on_tableWidget_itemSelectionChanged()
{

    ui->deleteButton->setEnabled(true);

    if(0!=changedSet.count(ui->tableWidget->selectedItems()[1]->row())){
        ui->editButton->setEnabled(true);
    }else{
        ui->editButton->setEnabled(false);
    }

}


void MemberDialog::on_deleteButton_clicked()
{
    ui->tableWidget->removeRow(ui->tableWidget->selectedItems()[0]->row());
    ui->tableWidget->selectedRanges();
    ui->tableWidget->setCurrentItem(NULL);
    ui->deleteButton->setEnabled(false);
    //ui->editButton->setEnabled(false);

}

void MemberDialog::on_editButton_clicked()
{
    ui->tableWidget->editItem( ui->tableWidget->selectedItems()[1]);

    ui->editButton->setEnabled(true);
    ui->saveButton->setEnabled(true);
}

void MemberDialog::on_saveButton_clicked()
{
    ui->tableWidget->closePersistentEditor(ui->tableWidget->selectedItems()[1]);
    ui->editButton->setEnabled(false);
    std::set<int>::iterator it;

    bool haveEmpty=false;

    for(it=changedSet.begin();it!= changedSet.end();it++ ){
        //Todo  save added member
        QTableWidgetItem *id = ui->tableWidget->item(*it,0);
        QTableWidgetItem *name = ui->tableWidget->item(*it,1);
        if(name->text().toStdString()==""){
            haveEmpty =true;
        }

    }

    if(!haveEmpty){
        for(it=changedSet.begin();it!= changedSet.end();it++ ){
            //Todo  save added member
            QTableWidgetItem *id = ui->tableWidget->item(*it,0);
            QTableWidgetItem *name = ui->tableWidget->item(*it,1);
            mm->addMember(id->text().toStdString(),name->text().toStdString());
        }
        ui->saveButton->setEnabled(false);
        changedSet.clear();
    }

    emit saveMember();

}

void MemberDialog::on_addButton_clicked()
{
    std::string id= mm->getNewId();
    int count = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(count+1);
    QTableWidgetItem *no = new QTableWidgetItem(QString::fromStdString(mm->getNewId()));
    no->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
    count%2!=0?no->setBackgroundColor(GRAY):no->setBackgroundColor(WHITE);
    no->setTextColor(BLACK);
    no->setFont(QFont("Helvetica",14));
    ui->tableWidget->setItem(count,0,no);

    QTableWidgetItem *name = new QTableWidgetItem("");
    name->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
    count%2!=0?name->setBackgroundColor(GRAY):name->setBackgroundColor(WHITE);
    name->setTextColor(BLACK);
    name->setFont(QFont("Helvetica",14));
    ui->tableWidget->setItem(count,1,name);

    ui->tableWidget->setCurrentItem(name);
    ui->tableWidget->editItem( ui->tableWidget->selectedItems()[1]);

    ui->tableWidget->scrollToBottom();
    changedSet.insert(count);

    ui->saveButton->setEnabled(true);
}

void MemberDialog::on_tableWidget_itemPressed(QTableWidgetItem *item)
{
    if(0!=changedSet.count(item->row())){
        ui->editButton->setEnabled(true);
    }else{
        ui->editButton->setEnabled(false);
    }
}

void MemberDialog::closeEvent(QCloseEvent * event){
    if(changedSet.size()!=0){
        event->ignore();
    }else{
        event->accept();
    }
}

void MemberDialog::showEvent(QShowEvent *event){
    std::cout<<"mem show" <<std::endl;
    QTableWidget * mem = ui->tableWidget;
    mem->setRowCount(mm->count());
    mem->setColumnWidth(0,mem->width()*0.45);
    mem->setColumnWidth(1,mem->width()*0.56);
    mem->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);

    for (int i=0;i<mm->count();i++){
        QTableWidgetItem *no = new QTableWidgetItem(QString::fromStdString(mm->getMember(i).id));
        no->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        i%2!=0?no->setBackgroundColor(GRAY):no->setBackgroundColor(WHITE);
        no->setTextColor(BLACK);
        no->setFont(QFont("宋体",14));
        ui->tableWidget->setItem(i,0,no);

        QTableWidgetItem *name = new QTableWidgetItem(QString::fromStdString(mm->getMember(i).name));
        name->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
        i%2!=0?name->setBackgroundColor(GRAY):name->setBackgroundColor(WHITE);
        name->setTextColor(BLACK);
        name->setFont(QFont("宋体",14));
        ui->tableWidget->setItem(i,1,name);
    }


    ui->addButton->setFocus();
    ui->deleteButton->hide();

}

void MemberDialog::on_exitButton_clicked()
{
    if(changedSet.size()!=0){

    }else{
        this->close();
    }
}
